﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using LuWeibo.Models;
using Microsoft.Phone.Shell;
using Code4WP7SDK;
using LuWeiboSDK;
using LuWeibo.ViewModels;
using Microsoft.Phone.Tasks;
using Code4WP7SDK.Controls;
using System.Windows.Data;

namespace LuWeibo.Views
{
    public partial class LuHomePage : PhoneApplicationPage
    {
        public LuHomePage()
        {
            InitializeComponent();
            //一开始加载首页微博信息
            ViewModelLocator.StausVMStatic.ShowWeiboExecute();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //this.ApplicationBar = CreateBar();
            this.SquareInfo.SquareList.SelectedIndex = -1;
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            App app = Application.Current as App;
            app.Exit();
        }

        private void AtMineBtn_Click(object sender, RoutedEventArgs e)
        {
            CommentsInfo.MineList.Visibility = Visibility.Visible;
            CommentsInfo.CommentsList.Visibility = Visibility.Collapsed;
            CommentsInfo.StoreList.Visibility = Visibility.Collapsed;
        }

        private void CommentsBtn_Click(object sender, RoutedEventArgs e)
        {
            CommentsInfo.CommentsList.Visibility = Visibility.Visible;
            CommentsInfo.MineList.Visibility = Visibility.Collapsed;
            CommentsInfo.StoreList.Visibility = Visibility.Collapsed;
        }

        private void StoreBtn_Click(object sender, RoutedEventArgs e)
        {
            CommentsInfo.StoreList.Visibility = Visibility.Visible;
            CommentsInfo.CommentsList.Visibility = Visibility.Collapsed;
            CommentsInfo.MineList.Visibility = Visibility.Collapsed;
        }

        private void Panorama_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            //parnorama 选择时
            //第一加载时选择相应的panoramaitem才加载数据并保存数据到sqlce中
            // 再次选择时先读sqlce中的然后再请求新数据
            if (LuPanorama.SelectedIndex == 0) //首页
            {
                ViewModelLocator.StausVMStatic.ShowWeiboExecute();
            }
            else if (LuPanorama.SelectedIndex == 1) //信息
            {
                ViewModelLocator.MainStatic.GetAtMineWeiboExecute();
            }
        }

        private ApplicationBar CreateBar()
        {
            ApplicationBar appBar = new ApplicationBar();
            appBar.Mode = ApplicationBarMode.Minimized;
            appBar.IsMenuEnabled = true;
            appBar.IsVisible = true;
            appBar.Opacity = 0.5;
            Binding binding = new Binding();
            binding.Source = Application.Current.Resources["Theme"];
            binding.Path = new PropertyPath("Instance.BaseBrush");
            //创建刷新按钮
            ApplicationBarIconButton refreshBtn = new ApplicationBarIconButton();
            refreshBtn.IsEnabled = true;
            refreshBtn.IconUri = new Uri("/Images/images_appbar_refresh.png", UriKind.Relative);
            refreshBtn.Text = "刷新";
            appBar.Buttons.Add(refreshBtn);
            //发表新微薄
            ApplicationBarIconButton publishBtn = new ApplicationBarIconButton();
            publishBtn.IsEnabled = true;
            publishBtn.IconUri = new Uri("/Images/images_appbar_newweibo.png", UriKind.Relative);
            publishBtn.Text = "新微博";
            publishBtn.Click += publishBtn_Click;
            appBar.Buttons.Add(publishBtn);
            //查看个人微薄主页 -- 在首页中包含了我的资料因此删除掉该功能按钮 但是仍然需要一个pivot来显示其他用户的资料
            ApplicationBarIconButton myInfoBtn = new ApplicationBarIconButton();
            myInfoBtn.IsEnabled = true;
            myInfoBtn.IconUri = new Uri("/Images/images_appbar_personalprofile.png", UriKind.Relative);
            myInfoBtn.Text = "我的资料";
            myInfoBtn.Click += myInfoBtn_Click;
            appBar.Buttons.Add(myInfoBtn);
            //搜素功能
            ApplicationBarIconButton searchBtn = new ApplicationBarIconButton();
            searchBtn.IsEnabled = true;
            searchBtn.IconUri = new Uri("/Images/images_appbar_search.png", UriKind.Relative);
            searchBtn.Text = "搜索";
            appBar.Buttons.Add(searchBtn);
            //皮肤管理
            ApplicationBarMenuItem iconMenu = new ApplicationBarMenuItem();
            iconMenu.IsEnabled = true;
            iconMenu.Text = "皮肤管理";
            iconMenu.Click += iconMenu_Click;
            appBar.MenuItems.Add(iconMenu);

            //设置
            ApplicationBarMenuItem settingMenu = new ApplicationBarMenuItem();
            settingMenu.IsEnabled = true;
            settingMenu.Text = "设置";
            appBar.MenuItems.Add(settingMenu);
            return appBar;
        }

        void iconMenu_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/Other/SkinsManagePage.xaml", UriKind.Relative));
        }


        void myInfoBtn_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/Profile/UsersStatusPage.xaml?uid=" + Constant.UID, UriKind.Relative));
        }

        /// <summary>
        /// 发布微博Appbar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void publishBtn_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/Status/PostStatusPage.xaml",UriKind.Relative));
        }
    }
}